home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1602_nichildren.c < prev    next >
Text File  |  1995-11-28  |  3KB  |  109 lines

  1. /*
  2.  * nichildren
  3.  *
  4.  * usage: nichildren domain directory
  5.  * compile with: cc -o nichildren nichildren.c
  6.  *
  7.  * This program lists the id and instance number of each of the
  8.  * child directories of a given directory.  For each child, it
  9.  * looks up the parent and prints its id and instance too.
  10.  *
  11.  * Sample output:
  12.  *
  13.  *    myhost> nichildren . /users
  14.  *    id = 25  instance = 4   id = 24  instance = 9
  15.  *    id = 27  instance = 2   id = 24  instance = 9
  16.  *    id = 29  instance = 2   id = 24  instance = 9
  17.  *    id = 31  instance = 2   id = 24  instance = 9
  18.  *    id = 33  instance = 2   id = 24  instance = 9
  19.  *    id = 35  instance = 2   id = 24  instance = 9
  20.  *    id = 37  instance = 2   id = 24  instance = 9
  21.  *    id = 39  instance = 4   id = 24  instance = 9
  22.  *
  23.  * Author: Marc Majka
  24.  * You may freely copy, distribute and reuse the code in this example.  
  25.  * NeXT disclaims any warranty of any kind, expressed or implied, as to 
  26.  * its fitness for any particular use.
  27.  *
  28.  */
  29.  
  30. #import <ansi/stdio.h>
  31. #import <bsd/errno.h>
  32. #import <netinfo/ni.h>
  33.  
  34. main(int argc, char *argv[]) {
  35.  
  36.     int             in;
  37.     ni_idlist       ilist;
  38.     void            *dom;
  39.     ni_id           dir, childdir, parentdir;   
  40.     ni_status       ret;
  41.     ni_index        parentid;
  42.  
  43.     /* check usage */
  44.     if (argc < 3) {
  45.         fprintf(stderr, "usage: %s domain directory\n", argv[0]);
  46.         exit(1);
  47.     }
  48.  
  49.     /* argv[1] should be a domain name */
  50.     ret = ni_open(NULL, argv[1], &dom);
  51.     if (ret != NI_OK) {
  52.         fprintf(stderr, "ni_open: %s\n", ni_error(ret));
  53.         exit(1);
  54.     }
  55.  
  56.     /* argv[2] should be a directory specification */
  57.     ret = ni_pathsearch(dom, &dir, argv[2]);
  58.     if (ret != NI_OK) {
  59.         fprintf(stderr, "ni_pathsearch: %s\n", ni_error(ret));
  60.         exit(1);
  61.     }
  62.  
  63.     /* find subdirectories */
  64.     ret = ni_children(dom, &dir, &ilist);
  65.     if (ret != NI_OK) {
  66.         fprintf(stderr, "ni_children: %s\n", ni_error(ret));
  67.         exit(1);
  68.     }
  69.  
  70.     /* for each entry */
  71.     for (in = 0; in <  ilist.ni_idlist_len; in++) {
  72.         childdir.nii_object = ilist.ni_idlist_val[in];
  73.  
  74.         /* fetch the instance number for this directory */
  75.         ret = ni_self(dom, &childdir);
  76.         if (ret != NI_OK) {
  77.             fprintf(stderr, "ni_self: %s\n", ni_error(ret));
  78.             exit(1);
  79.         }
  80.  
  81.         /* print the directory ID and instance number */
  82.         printf("id = %ld  instance = %ld\t",
  83.             childdir.nii_object, childdir.nii_instance);
  84.  
  85.         /* now back up and find the child's parent */
  86.         ret = ni_parent(dom, &childdir, &parentid);
  87.         if (ret != NI_OK) {
  88.             fprintf(stderr, "ni_parent: %s\n", ni_error(ret));
  89.             exit(1);
  90.         }
  91.  
  92.         /* fetch the instance number for the parent */
  93.         parentdir.nii_object = parentid;
  94.         ret = ni_self(dom, &parentdir);
  95.         if (ret != NI_OK) {
  96.             fprintf(stderr, "ni_self: %s\n", ni_error(ret));
  97.             exit(1);
  98.         }
  99.  
  100.         /* print the parent's id and instance next to the child */
  101.         printf("id = %ld  instance = %ld\n",
  102.             parentdir.nii_object, parentdir.nii_instance);
  103.     }
  104.  
  105.     /* clean up */
  106.     ni_free(dom);
  107.  
  108.     exit(0);
  109. }